package com.callen.quartz;

import java.io.IOException;

import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.callen.bean.UserBean;
import com.callen.service.GeneralControlService;
import com.callen.service.WebSSHService;
import com.callen.service.imp.GeneralControlServiceImpl;
import com.callen.service.imp.WebSSHServiceImpl;
import com.callen.utils.ApplicationUser;
import com.callen.utils.SpringUtils;

@DisallowConcurrentExecution
public class ReadShellOutputStreamJob implements Job{
	private static Logger log=LoggerFactory.getLogger(ReadShellOutputStreamJob.class);
	private WebSSHService webSSHService=SpringUtils.getBean(WebSSHServiceImpl.class);
	private ApplicationUser applicationUser=SpringUtils.getBean(ApplicationUser.class);
	private GeneralControlService generalControlService=SpringUtils.getBean(GeneralControlServiceImpl.class);

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		String jobName=context.getJobDetail().getKey().getName();
		log.debug("jobName:{}",jobName);
		String userId=ApplicationUser.jobUserMap.get(jobName);
		log.debug("userId:{}",userId);
		UserBean userBean=applicationUser.getUser(userId);
		try {
			boolean flag=webSSHService.checkSession(userBean);
			log.debug("flag:{}",flag);
			if(flag) {
				webSSHService.readInputStream(userId);
			}else {
				generalControlService.destory(userId);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
